var fnurlFkey = '';

function fnurl(url, method) {
	if (!method) method = 'navigateTo';
	let _fnurlFkey = `${url}_${method}`;
	if (fnurlFkey == _fnurlFkey) return;

	fnurlFkey = _fnurlFkey;
	setTimeout(() => {
		fnurlFkey = '';
	}, 800);

	while (url.indexOf('wxapp://') == 0) {
		let appId = url.split('wxapp://')[1].split('/')[0];
		let path = url.replace('wxapp://' + appId + '/', '');

		//
		if (!appId || !path) break;

		uni.navigateToMiniProgram({
			appId,
			path,
		});

		return;
	}

	if (url.indexOf('/pages/') == 0) {
		return uni[method]({
			url
		});
	}

	// #ifdef H5
	window.location.href = url;
	return;
	// #endif

	url = url.replace('http://', 'https://').replace('hd55.cmmarketing.com.cn', 'hd.lmjx.net').replace(
		'hd20.cmmarketing.com.cn', 'inmark.lmjx.net');
	return uni[method]({
		url: '/pages/webview?url=' + encodeURIComponent(url),
	});
}


function back(url, method) {
	if (getCurrentPages().length > 1) return uni.navigateBack();
	url && fnurl(url, method);
}


const burlkpages = {
	
};

function burl(burl, method) {
	if (!burl) return;
	if (burl.trim() == '') return;

	console.log('====burl====', burl);
	if (burl.indexOf('wxapp://') == 0 || burl.indexOf('http://') == 0 || burl.indexOf('https://') == 0) return fnurl(
		burl);

	//同名路由
	let tmp = burl.split('?');
	let page = tmp[0];
	let props = tmp[1] ? tmp[1] : '';

	if (burlkpages[page]) {
		let tpage = burlkpages[page];
		burl = tpage + (props ? ((tpage.indexOf('?') > 0 ? '&' : '?') + props) : '');
	}

	console.log('===to=burl', burl);
	if (burl.indexOf('main/') === 0) method = 'switchTab';
	
	fnurl('/pages/' + burl, method ? method : null);
}

function login() {
	fnurl('/pages/account/login');
}


function home() {
	fnurl('/pages/main/index', 'switchTab');
}

function videoPlayer(src, width, height, poster) {
	let _src = encodeURIComponent(src);
	let _poster = encodeURIComponent(poster);
	let _burl = `videoplayer?src=${_src}&width=${width}&height=${height}&poster=${_poster}`;
	return burl(_burl);
}

function inputer(props) {
	let ps = [];
	Object.keys(props).map(it => {
		ps.push(it + '=' + encodeURIComponent(props[it]));
	});

	return burl('pub/input?' + ps.join('&'));
}

function feedback(props) {
	let ps = [];
	Object.keys(props).map(it => {
		ps.push(it + '=' + encodeURIComponent(props[it]));
	});

	return burl('pub/feedback?' + ps.join('&'));
}

module.exports = {
	url: fnurl,
	back,
	burl,
	login,
	home,
	videoPlayer,
	inputer,
}
